package ocean.chinacraft.common.world.block.function;

import ocean.chinacraft.common.world.block.properties.PropertiesMisc;
import ocean.chinacraft.common.world.block.function.container.JadeWorkbenchBlockContainer;
import ocean.chinacraft.common.ModTileEntity;
import ocean.chinacraft.common.tileentity.function.JadeWorkbenchBlockTile;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.NetworkHooks;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * 玉石加工台
 *
 * @author zhang
 * @since 2021/11/19
 */
public class JadeWorkbenchBlock extends Block {

    public JadeWorkbenchBlock() {
        super(PropertiesMisc.defaultFunction());
    }

    @Override
    public ActionResultType use(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult rayTraceResult) {
        if (!world.isClientSide()) {
            TileEntity tileEntity = world.getBlockEntity(pos);
            if (tileEntity instanceof JadeWorkbenchBlockTile) {
                INamedContainerProvider containerProvider = createContainerProvider(world, pos);
                NetworkHooks.openGui(((ServerPlayerEntity) player), containerProvider, tileEntity.getBlockPos());
            } else {
                throw new IllegalStateException("Our Container provider is missing!");
            }
        }
        return ActionResultType.SUCCESS;
    }

    private INamedContainerProvider createContainerProvider(World worldIn, BlockPos pos) {
        return new INamedContainerProvider() {
            @Nonnull
            @Override
            public ITextComponent getDisplayName() {
                return new TranslationTextComponent("screen.chinacraft.Jade_workbench_block");
            }

            @Override
            public Container createMenu(int i, @Nonnull PlayerInventory playerInventory, @Nonnull PlayerEntity playerEntity) {
                return new JadeWorkbenchBlockContainer(i, worldIn, pos, playerInventory, playerEntity);
            }
        };
    }

    @Nullable
    @Override
    public TileEntity createTileEntity(BlockState state, IBlockReader world) {
        return ModTileEntity.JADE_WORKBENCH_BLOCK_TILE.get().create();
    }

    @Override
    public void onRemove(BlockState state, World world, BlockPos pos, BlockState state1, boolean flag) {
        if (!state.is(state1.getBlock())) {
            TileEntity tileentity = world.getBlockEntity(pos);
            if (tileentity instanceof JadeWorkbenchBlockTile) {
                InventoryHelper.dropContents(world, pos, (JadeWorkbenchBlockTile) tileentity);
                world.updateNeighbourForOutputSignal(pos, this);
            }
            super.onRemove(state, world, pos, state1, flag);
        }
    }

    @Override
    public boolean hasTileEntity(BlockState state) {
        return true;
    }
}
